package com.molichuxing.services.infrastructure.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.exception.BizException;
import com.molichuxing.framework.utils.ObjectCastUtil;
import com.molichuxing.services.infrastructure.bean.ComplaintBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.ComplaintMapper;
import com.molichuxing.services.infrastructure.dto.request.create.ComplaintCreateDto;
import com.molichuxing.services.infrastructure.dto.request.modify.ComplaintModifyDto;
import com.molichuxing.services.infrastructure.dto.response.ComplaintDto;
import com.molichuxing.services.infrastructure.service.ComplaintService;

/**
 * 
 * @author liyg
 * 用户投诉服务实现类
 * 2019-08-13
 * 
 */
@Service("complaintService")
public class ComplaintServiceImpl implements ComplaintService{
	
	@Autowired
	private ComplaintMapper complaintMapper;

	/**
	 * 获取用户投诉详情
	 * 
	 * @param complaintId
	 *            投诉id
	 * @return
	 */
	@Override
	public ComplaintDto getComplaint(Integer complaintId) {
		
		ComplaintBean complaint = complaintMapper.getById(complaintId);
		
		ComplaintDto dto = Convert.toComplaintDto(complaint);
		
		return dto;
	}
	
	 /**
	  * 获取用户投诉详情
	  * 
	  * @param userId
	  *            用户id
	  * @return
	  */
	@Override
	public ComplaintDto getComplaintByUserId(Integer userId) {
		
		List<ComplaintBean> list = complaintMapper.getComplaintByUserId(userId);
		ComplaintBean complaint=null;
		if( null != list && list.size() > 0 ) {
			complaint=list.get(0);
		}
		ComplaintDto dto = Convert.toComplaintDto(complaint);
		
		return dto;
	}
	
	 /**
	  * 获取投诉数量
	  * @param status
	  * @return
	  */
	@Override
	public Long getCountByStatus(Integer status) {
		return complaintMapper.getCountByStatus(status);
	}

	/**
	 * 编辑投诉
	 * 
	 * @param modifyDto
	 *            需要修改的数据实体
	 * @return
	 */
	@Override
	public Boolean modifyComplaint(ComplaintModifyDto modifyDto) {
		
		ComplaintBean complaint = complaintMapper.getById(modifyDto.getId());
		if(null == complaint) {
			throw new BizException(101, "不存在该条信息");
		}
		
		//数据转换
		complaint = Convert.toComplaint(modifyDto);
		
		int num = complaintMapper.mdifyStatusById(complaint);
		
		return num == 1;
	}
	
	/**
	 * 提交投诉信息
	 * 
	 * @param createDto
	 *            
	 * @return
	 */
	@Override
	public Boolean createComplaint(ComplaintCreateDto createDto) {
		ComplaintBean complaint = new ComplaintBean();
		BeanUtils.copyProperties(createDto, complaint);
		int num = complaintMapper.create(complaint);
		return num == 1;
	}

	/**
	 * 获取投诉列表
	 * 
	 * @param map
	 *            参数
	 * @param key:userId
	 *            Integer 用户id 非必填
	 * @param key:type
	 *            Integer 投诉方式 非必填
	 * @param key:status
	 *            Integer 投诉处理状态 非必填
	 * @param key:startTime
	 *            String 投诉开始时间 非必填
	 * @param key:endTime
	 *            String 投诉开始时间 非必填
	 * @param key:pageNum
	 *            Integer 当前页
	 * @param key:pageSize
	 *            Integer 每页显示条数
	 * @return
	 */
	@Override
	public Paged<ComplaintDto> getComplaintList(Map<String, Object> map) {

		// 查询总条数
		long total = complaintMapper.getComplaintPageCount(map);
		if(total <= 0) {
			return null;
		}
		
		/*分页查询集合*/ 
		Integer pageNum = ObjectCastUtil.castInteger(map.get("pageNum"));
		Integer pageSize = ObjectCastUtil.castInteger(map.get("pageSize"));
		List<ComplaintBean> list = complaintMapper.getComplaintPage(map,pageNum,pageSize);
		List<ComplaintDto> complaintDtoList = Convert.toComplaintDto(list);
		Paged<ComplaintDto> paged = new Paged<>(complaintDtoList, total,pageSize);
		
		return paged;
	}
	
	

}
